/*
 * GPB registers, led is connected to GPB
 */
#define GPBCON   (*(volatile unsigned long *)0x56000010)
#define GPBDAT   (*(volatile unsigned long *)0x56000014)
#define GPBUP    (*(volatile unsigned long *)0x56000018)

#define LED0     5    /* LED0 use GPB5 */
#define LED1     6    /* LED1 use GPB6 */
#define LED2     7    /* LED2 use GPB7 */
#define LED3     8    /* LED3 use GPB8 */

#define GPB5_OUT (1 << (LED0) * 2)
#define GPB6_OUT (1 << (LED1) * 2)
#define GPB7_OUT (1 << (LED2) * 2)
#define GPB8_OUT (1 << (LED3) * 2)

/*
 * GPG registers, key is connected to GPG
 */
#define GPGCON    (*(volatile unsigned long *)0x56000060)
#define GPGDAT    (*(volatile unsigned long *)0x56000064)
#define GPGUP     (*(volatile unsigned long *)0x56000068)

#define KEY0      0    /* KEY0 use GPG0 */
#define KEY1      3    /* KEY1 use GPG3 */
#define KEY2      5    /* KEY2 use GPG5 */
#define KEY3      6    /* KEY3 use GPG6 */
#define KEY4      7    /* KEY4 use GPG7 */
#define KEY5      11   /* KEY5 use GPG11 */

#define GPG0_IN   ~(3 << (KEY0) * 2)
#define GPG3_IN   ~(3 << (KEY1) * 2)
#define GPG5_IN   ~(3 << (KEY2) * 2)
#define GPG6_IN   ~(3 << (KEY3) * 2)
#define GPG7_IN   ~(3 << (KEY4) * 2)
#define GPG11_IN  ~(3 << (KEY5) * 2)

#define LED_ON(n)    GPBDAT &= ~(1<<(n))
#define LED_OFF(n)   GPBDAT |= 1<<(n)
#define KEY_OFF(n)   GPGDAT & 1<<(n)

int main()
{
    unsigned long key_state;
    
    /* set related GPB pin for led as output */
    GPBCON = GPB5_OUT | GPB6_OUT | GPB7_OUT | GPB8_OUT;

    /* set related GPG pin for key as input */
    GPGCON  = GPG0_IN & GPG3_IN & GPG5_IN & GPG6_IN & GPG7_IN & GPG11_IN;

    while (1) {
        if (KEY_OFF(KEY0)) {
            LED_OFF(LED0);
        } else {
            LED_ON(LED0);
        }
        
        if (KEY_OFF(KEY1)) {
            LED_OFF(LED1);
        } else {
            LED_ON(LED1);
        }
        
        if (KEY_OFF(KEY2)) {
            LED_OFF(LED2);
        } else {
            LED_ON(LED2);
        }
        
        if (KEY_OFF(KEY3)) {
            LED_OFF(LED3);
        } else {
            LED_ON(LED3);
        }
    }
    
    return 0;
}
